/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.gui.wizard;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

/**
 * This class represents a JList with active checkboxes
 *
 * @author Basil Shikin
 *
 */
class CheckboxList<T> 
extends JList
{
    private static final long serialVersionUID = -5092799880616977631L;

    protected static Border   noFocusBorder    = new EmptyBorder(1, 1, 1, 1);

    protected final DefaultListModel model;
    
    public CheckboxList()
    {
        setCellRenderer(new CellRenderer());

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e)
            {
                if ( CheckboxList.this.isEnabled() )
                {
                    int index = locationToIndex(e.getPoint());

                    if ( index != -1 )
                    {
                        JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index);
                        checkbox.setSelected(!checkbox.isSelected());
                        repaint();
                    }
                }
            }
        });

        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        model = new DefaultListModel();
        setModel( model );
    }
    
    public void addObject( final T object, final String objectName )
    {
        JCheckBox checkBox = new JCheckBox();
        checkBox.setText( objectName );
        checkBox.setSelected( true );
        checkBox.putClientProperty( "object", object );
        
        model.addElement( checkBox );
    }

    @SuppressWarnings("unchecked")
    public List<T> getSelectedObjects()
    {
        final List<T> result = new ArrayList<T>( model.getSize() );
        
        for ( int i = 0; i < model.getSize(); i++ )
        {
            final JCheckBox checkbox = (JCheckBox)model.getElementAt( i );
            
            if ( checkbox.isSelected() )
            {
                result.add( (T)checkbox.getClientProperty("object") );
            }
        }
        
        return result;
    }
    
    protected class CellRenderer 
        implements ListCellRenderer
    {
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
        {
            JCheckBox checkbox = (JCheckBox) value;
            checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground());
            checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground());
            checkbox.setEnabled(isEnabled());
            checkbox.setFont(getFont());
            checkbox.setFocusPainted(false);
            checkbox.setBorderPainted(true);
            checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
            return checkbox;
        }
    }
}
